﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Collections.Generic;
using System.Globalization;
using ConfigGen.ConsoleApp.Core.CommandLineOptionCore;
using ConfigGen.Core;

namespace ConfigGen.ConsoleApp.Core.CommandLineOptions
{
    /// <summary>
    /// Console argument handler to handle the "settings file" command line option. This processor is responsible for setting the
    /// <see cref="Preferences.SettingsFile"/> property on the settings object.
    /// </summary>
    public class ForceName : ICommandLineOption<ConsoleAppPreferences>
    {
        private string _fileName;

        #region IConsoleArgHandler<Preferences> Members

        /// <summary>
        /// Gets the short name of the command line argument, without the leading hyphen; for this processor: "n".
        /// </summary>
        /// <value>"n"</value>
        public string ShortName
        {
            get { return "n"; }
        }

        /// <summary>
        /// Gets the long name of the command line argument, without the leading hyphens; for this processor: "force-name"
        /// </summary>
        /// <value>"force-name"</value>
        public string LongName
        {
            get { return "force-name"; }
        }

        /// <summary>
        /// Gets a help string containing the args usage for this processor: "&lt;filename&gt;"
        /// </summary>
        /// <value>"&lt;filename&gt;"</value>
        public string ArgsUsage
        {
            get { return "<filename>"; }
        }

        /// <summary>
        /// Gets help text for this command line option.
        /// </summary>
        /// <value>Help text</value>
        public string HelpText
        {
            get { return "Forces the filename of the any generated config files, ignoring the name specified in the settings"; }
        }

        /// <summary>
        /// Applies the option represented by this instance to the supplied settings instance.
        /// </summary>
        /// <param name="settings">The settings object.</param>
        public void ApplyOptionToSettings(ConsoleAppPreferences settings)
        {
            settings.ForcedFilename = _fileName;
        }

        /// <summary>
        /// Processes the argument queue by consuming (dequeuing) any arguments used by this instance. For this type of processor, 
        /// a single argument is consumed from the queue, being the settings file path.
        /// </summary>
        /// <param name="args">Args queue from which to consume args</param>
        /// <exception cref="ArgumentException">Thrown if args queue is empty.</exception>
        public void ConsumeArgumentsFromQueue(Queue<string> args)
        {
            if (args.Count < 1)
            {
                var msg = string.Format(CultureInfo.InvariantCulture,
                                        "filename switch [-{0} or --{1}] expects a filename argument",
                                        ShortName,
                                        LongName);
                throw new ArgumentException(msg);
            }

            _fileName = args.Dequeue();
        }

        #endregion
    }
}
